package edu.ec.java02.group02.controller;

import edu.ec.java02.group02.domain.ProductInfo;
import edu.ec.java02.group02.service.ManagerService;
import edu.ec.java02.group02.util.Page;
import edu.ec.java02.group02.util.ResultMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    ManagerService managerService;

    /**
     * 查看商品列表
     * @param request
     * @return 商品列表JSP页面
     */
    @RequestMapping("/productList")
    public String productList(HttpServletRequest request){
        return "product/product-list";
    }

    /**
     *查看商品列表-分页
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/getProductPage")
    @ResponseBody
    public ResultMap<List<ProductInfo>> getProductPage(Page page,int limit,HttpServletRequest request){
        page.setRows(limit);
        page.setProductName(request.getParameter("productName"));
        page.setProductType(request.getParameter("productType"));
        page.setProductState(request.getParameter("productState"));
        List<ProductInfo> productInfoList = managerService.selectProductListPage(page);
        int totals = managerService.selectProductListPageCount(page);
        page.setTotalRecord(totals);
        return new ResultMap<List<ProductInfo>>("",productInfoList,0,totals);
    }

    /**
     * 进入添加商品页面
     * @return 添加商品页面
     */
    @RequestMapping("/addProductHtm")
    public String addProductHtm(){ return  "product/product-add";}

    /**
     * 接收前台商品数据并传递给ManagerService进行事务处理
     * @param productImage
     * @param request
     * @return
     */
    @PostMapping("/addProduct")
    public String addProduct(MultipartFile productImage,HttpServletRequest request){
        ProductInfo productInfo = new ProductInfo();
        //从前台接收商品数据
        productInfo.setProductName(request.getParameter("productName"));
        productInfo.setProductInventory(Integer.parseInt(request.getParameter("productInventory")));
        productInfo.setProductType(request.getParameter("productType"));
        productInfo.setProductPrice(Double.parseDouble(request.getParameter("productPrice")));
        productInfo.setProductDescription(request.getParameter("productDescription"));
        int flag = managerService.saveProduct(productInfo,productImage,request.getServletContext().getRealPath("/productImages"));
        return "product/product-add";
    }

    @RequestMapping("updateProductHtm")
    public String updateProduct(String productId,HttpServletRequest request){
        ProductInfo productInfo = managerService.getProductById(Integer.parseInt(productId));
        request.setAttribute("Product",productInfo);
        return "product/product-edit";
    }

    @RequestMapping("updateProduct")
    public String updateProduct(MultipartFile productImage,HttpServletRequest request){
        ProductInfo productInfo = new ProductInfo();
        //从前台接收商品数据
        productInfo.setProductId(Integer.parseInt(request.getParameter("productId")));
        productInfo.setProductName(request.getParameter("productName"));
        productInfo.setProductInventory(Integer.parseInt(request.getParameter("productInventory")));
        productInfo.setProductType(request.getParameter("productType"));
        productInfo.setProductPrice(Double.parseDouble(request.getParameter("productPrice")));
        productInfo.setProductDescription(request.getParameter("productDescription"));
        int flag = managerService.modifyProductInfo(productInfo,productImage,request.getServletContext().getRealPath("/productImages"));
        return "product/product-edit";
    }

    @RequestMapping("updateProductState")
    @ResponseBody
    public String updateProductState(String productId,String productState){
        return managerService.modifyProductStatus(Integer.parseInt(productId),productState);
    }

    @RequestMapping("delProduct")
    @ResponseBody
    public String delProduct(String productIds){
        return managerService.delProductInfo(productIds);
    }
}
